<template>
  <div>
    <van-form @submit="onSubmit">
      <van-field
        v-model="userInfo.userName"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[
          { required: true, message: '请填写用户名' }
        ]"
      />
      <van-field
        v-model="userInfo.pwd"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
export default {
  data () {
    return {
      userInfo: {
        userName: '',
        pwd: ''
      }
    }
  },
  methods: {
    onSubmit () {
      // 点击登录
      setTimeout(() => {
        // 请求成功 返回 用户信息 以及 token
        const userInfo = {
          nickName: '小甜甜',
          avatar: 'https://img.tupianzj.com/uploads/allimg/202010/9999/rndcd04c8117.jpg'
        }
        const token = 'dwuhdw327439e3dwhre'
        localStorage.setItem('userInfo', JSON.stringify(userInfo))
        localStorage.setItem('token', token)
        // 重定向 从哪来 回哪去
        if (this.$route.query.from) {
          this.$router.push(this.$route.query.from)
        } else {
          this.$router.push('/home')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
